#include "h_cc2640r2f_gpio.h"

#include "h_cc2640r2f_define.h"

#include <stdio.h>
#include <string.h>

//#include "s_msg_ble.h"
#include <ti/drivers/pin/PINCC26XX.h>
//#include "s_lockcontrol.h"
////////////////////////////////////////////////////////////////////////////////

static const PIN_Config H_CC2640R2F_GPIO_CFG[] =
{
    /*
    * Output GPIO part
    * */
    /* PSAM Power Supply */
    S_PSAMCARD_POWERCONTRL_IO | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
    /* PSAM Reset Control */
    S_PSAMCARD_RST_IO | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
    /* MCU wake notify */
    S_WAKE_UP_OUT | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,

    /*
     * Input GPIO part
     * */
    S_WAKE_UP_IN | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_PULLUP,
    //KeyBoard IRQ Pin
//    S_LOCK_KEYBOARD_IRQ_PIN | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_PULLUP,

#if LOCK_DEBUG_LED
    S_DEBUG_LED_IO | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_DRVSTR_MAX,
#endif

    PIN_TERMINATE
};

static PIN_Handle H_CC2640R2F_GPIO_Handle = NULL;
static PIN_State H_CC2640R2F_GPIO_State;

////////////////////////////////////////////////////////////////////////////////
static void H_CC2640R2F_GPIO_InputCallback(PIN_Handle hPin, PIN_Id pin_Id)
{
//    s_msg_ask_active_peripheral_irqway();
}

signed char H_CC2640R2F_GPIO_Init(void)
{
    H_CC2640R2F_GPIO_Handle = PIN_open(&H_CC2640R2F_GPIO_State, \
                                                H_CC2640R2F_GPIO_CFG);

    return 0;
}

signed char H_CC2640R2F_GPIO_IRQ_Enable(void)
{
    // For the input register
    PIN_registerIntCb(H_CC2640R2F_GPIO_Handle, H_CC2640R2F_GPIO_InputCallback);
    PIN_setConfig(H_CC2640R2F_GPIO_Handle, PIN_BM_IRQ, S_WAKE_UP_IN | PIN_IRQ_NEGEDGE);
#ifdef POWER_SAVING     
    PIN_setConfig(H_CC2640R2F_GPIO_Handle, PINCC26XX_BM_WAKEUP, S_WAKE_UP_IN | PINCC26XX_WAKEUP_NEGEDGE);
#endif
    return 0;
}

/*
    Set the Output GPIO value.
*/
signed char H_CC2640R2F_GPIO_OutputSet(unsigned char PinID, unsigned int value)
{
    if(NULL == H_CC2640R2F_GPIO_Handle)
    {
        return (-1);
    }
    PIN_setOutputValue(H_CC2640R2F_GPIO_Handle, PinID, value);
    return 0;
}

/*
    Toogle the Output GPIO
*/
signed char H_CC2640R2F_GPIO_OutputToggle(unsigned char PinID)
{
    if(NULL == H_CC2640R2F_GPIO_Handle)
    {
        return (-1);
    }
    PIN_setOutputValue(H_CC2640R2F_GPIO_Handle, PinID, \
                        (PIN_getOutputValue(PinID) == 0)?1:0);

    return 0;
}

///*
// * IIC-S part
// * */
//void H_CC2640R2F_GPIO_SDA_IN(void)
//{
//    PIN_setConfig(H_CC2640R2F_GPIO_Handle, \
//                        PIN_BM_OUTPUT_MODE | PIN_BM_INPUT_MODE, \
//                            H_CC2640R2F_IICS_SDA | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_NOPULL);
//}
//
//void H_CC2640R2F_GPIO_SDA_OUT(void)
//{
//    PIN_setConfig(H_CC2640R2F_GPIO_Handle, \
//                        PIN_BM_OUTPUT_MODE | PIN_BM_INPUT_MODE, \
//                            H_CC2640R2F_IICS_SDA | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX);
//}

